/**
 * 
 */
package es.ats.codington.festival.daos;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import es.ats.codington.festival.domain.Building;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Place.Real;
import es.ats.codington.festival.interfaces.daos.IPlaceDAO;
import es.ats.codington.festival.utils.JSONQuery;
import es.ats.codington.festival.utils.PlaceDBQuery;

/**
 * The Class PlaceDAOImpl.
 *
 * @author 5Cs
 */
public class PlaceDAOImpl implements IPlaceDAO{
	
	/** The adress. */
	private static String adress = "src/es/ats/codington/festival/utils/DBJson.json";
	
	/** The Constant Place_Insert. */
	private static final String Place_Insert = "Place_Insert";	
	
	/** The Constant Building_Insert. */
	private static final String Building_Insert = "Building_Insert";
	
	/** The Constant Place_Delete_Id. */
	private static final String Place_Delete_Id = "Place_Delete_Id";
	
	/** The Constant Building_Delete_Id. */
	private static final String Building_Delete_Id = "Building_Delete_Id";
	
	/** The Constant Place_Update_Id. */
	private static final String Place_Update_Id = "Place_Update_Id";
	
	/** The Constant Building_Update_Id. */
	private static final String Building_Update_Id = "Building_Update_Id";
	
	/** The Constant Search_All_Places. */
	private static final String Search_All_Places = "Search_All_Places";
	
	/** The Constant Search_Places_Id. */
	private static final String Search_Places_Id = "Search_Places_Id";
	
	
	/**
	 * Place dao impl.
	 */
	public void PlaceDAOImpl(){		
	}
	
	
//	Inserts 
	
	
	
/**
 * Insert place dao.
 *
 * @param conec the conec
 * @param insertPlace the insert place
 * @return true, if successful
 * @throws Exception the exception
 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#insertPlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Place)
 */
public boolean insertPlaceDAO(Connection conec, Place insertPlace) throws Exception {
		boolean full= false;
		
		//access to JSON 
	//	JSONQuery reader = new JSONQuery(adress);	
	    String chain = "INSERT INTO places (Name, Capacity, Description, Zone, Reale) values(?,?,?,?,?);";
	    
	    System.out.println("SQL->"+chain);	    
	    	int key = PlaceDBQuery.insPlaceTable(conec, insertPlace, chain);
	        if (key == 0){
	        //	System.out.println("nooooo -> insert Place");
	        	full = true;
	        }
	         else {
	        	 Real real = insertPlace.getReal();
	        	 chain = "INSERT INTO " + real + "("+ real +"Id) values(?);";
	        //	 chain = "INSERT INTO " + real + " (ZooId) values(?);";
	        	 //* chain = "INSERT INTO zoo (ZooId) values(?);" + real;
	        		//	 reader.readString("INSERT INTO "+real);	
	        	  key = PlaceDBQuery.insIdChild(conec, key, chain);
			  	        if (key == 0){
				        	/**System.out.println("nooooo -> insert "+real);*/
				        	full = true;
				        }
			  	       /** else System.out.println("SQL2-> Insert_"+real);*/
	         }

    
	    conec.close();
  		return full;
	}

	
	/**
	 * Insert place dao.
	 *
	 * @param conec the conec
	 * @param insertBuilding the insert building
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#insertPlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Building)
	 */
	@Override
	public boolean insertPlaceDAO(Connection conec, Building insertBuilding) throws Exception {
		boolean full= false;
		
		//access to JSON 
		//JSONQuery reader = new JSONQuery(adress);	
	    String chain = "INSERT INTO places (Name, Capacity, Description, Zone, Reale) values(?,?,?,?,?);";
	    
	   /** System.out.println("SQL->"+chain);	 */   
	    	int key = PlaceDBQuery.insBuildingTable(conec, insertBuilding, chain);
	    	
	        if (key == 0){
	    //    	System.out.println("nooooo -> Insert_Place");
	        	full = true;
	        }
	         else {
	        	 Real real= insertBuilding.getReal();
	        	 chain = "INSERT INTO building (BuildingId) values(?);";
	        	 int key2 = PlaceDBQuery.insIdChild(conec, key, chain);
				  	        if (key2 == 0){
					        //	System.out.println("nooooo -> insert Building");
					        	full = true;
					        }
				  	        else {
				  	        //	System.out.println("SQL2->"+chain);
				  	        	chain= "INSERT INTO "+real+"("+real+"Id) values(?);";

					        	  int key3 = PlaceDBQuery.insIdChild(conec, key, chain);
							  	        if (key3 == 0){
								       // 	System.out.println("nooooo -> insert sonSon");
								        	full = true;
								        }
							  	       /** else System.out.println("SQL3->"+chain);*/
					  	      }	        	  	        	 	        	 	     
	         }	          
	    conec.close();
  		return full;
	}
	
	
	//Deletes	
	/**
	 * Delete place dao.
	 *
	 * @param conec the conec
	 * @param deletePlace the delete place
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#deletePlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Place)
	 */
	@Override
	public boolean deletePlaceDAO(Connection conec, Place deletePlace) throws Exception{
		boolean full = false;
		JSONQuery reader = new JSONQuery(adress);	
		String chain = reader.readString("Delete_"+ deletePlace.getReal());
	//	System.out.println(chain+"  "+ deletePlace.getiD());
		full = PlaceDBQuery.delPlaceTable(conec, deletePlace.getiD(), chain);       
        if (full){
    	//	System.out.println("SQL->"+chain);
        	chain = reader.readString(Place_Delete_Id);
    	    full = PlaceDBQuery.delPlaceTable(conec, deletePlace.getiD(), chain);
    	    /** if (full) System.out.println("SQL2->"+chain);
    	     	else System.out.println("nooo -> Delete Place");*/
        }
        
       /** else System.out.println("nooo-> Delete Place");*/

	    conec.close();
  		return full;
	}
	
	
	/**
	 * Delete place dao.
	 *
	 * @param conec the conec
	 * @param deleteBuilding the delete building
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#deletePlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Building)
	 */
	@Override
	public boolean deletePlaceDAO(Connection conec, Building deleteBuilding) throws Exception{
		boolean full = false;
		//access to JSON 
		JSONQuery reader = new JSONQuery(adress);	
		String chain = reader.readString("Delete_"+ deleteBuilding.getReal());
		/**System.out.println(chain+"  "+ deleteBuilding.getiD());*/
		full = PlaceDBQuery.delPlaceTable(conec, deleteBuilding.getiD(), chain);       
        if (full){
    		/**System.out.println("SQL->"+chain);*/
        	chain = reader.readString(Building_Delete_Id);
    	    full = PlaceDBQuery.delPlaceTable(conec, deleteBuilding.getiD(), chain);
    	     if (full){
    	    	/** System.out.println("SQL2->"+chain);*/
    	    	 chain = reader.readString(Place_Delete_Id);
    	    	 full = PlaceDBQuery.delPlaceTable(conec, deleteBuilding.getiD(), chain);
    	    	/** if (full){
    	    		 System.out.println("SQL3->"+chain);
    	    	 }
    	    	 else System.out.println("no -> delete Place");*/
    	     }
    	    /** else System.out.println("no -> delete Bulding");*/
    	    	 

        }
        
       /** else System.out.println("nooo-> Delete sonSon");*/

	    conec.close();
  		return full;
	 }
	
	//Updates
	/**
	 * Update place dao.
	 *
	 * @param conec the conec
	 * @param updatePlace the update place
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#updatePlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Place)
	 */
	@Override
	public boolean updatePlaceDAO(Connection conec, Place updatePlace) throws Exception{
		
		//access to JSON 
		JSONQuery reader = new JSONQuery(adress);	
	    String chain = reader.readString(Place_Update_Id);
	    
	   /** System.out.println("SQL->"+chain);*/
			boolean full = PlaceDBQuery.updPlaceTable(conec, updatePlace, chain);
	    /**    if (full)System.out.println("siiiii -> update");
	           else System.out.println("nooooo -> update");*/
     
	    conec.close();
  		return full;
	}
	
	
	/**
	 * Update place dao.
	 *
	 * @param conec the conec
	 * @param updateBuilding the update building
	 * @return true, if successful
	 * @throws Exception the exception
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#updatePlaceDAO(java.sql.Connection, es.ats.codington.festival.domain.Building)
	 */
	@Override
	public boolean updatePlaceDAO(Connection conec, Building updateBuilding) throws Exception{
		
		//access to JSON 
		JSONQuery reader = new JSONQuery(adress);	
	    String chain = reader.readString(Building_Update_Id);
	    
	  /**  System.out.println("SQL->"+chain);*/
			boolean full = PlaceDBQuery.updBuildingTable(conec, updateBuilding, chain);
	     /**   if (full)System.out.println("siiiii -> update Building");
	           else System.out.println("nooooo -> update Building");*/
     
	    conec.close();
  		return full;
	}

	/**
	 * Show all place.
	 *
	 * @param conec the conec
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#showAllPlace(java.sql.Connection)
	 */
	@Override
	public boolean showAllPlace(Connection conec) {
		boolean full= false;
		
		//access to JSON 
		JSONQuery reader = new JSONQuery(adress);	
	    String chain = reader.readString(Search_All_Places);    
	    full = PlaceDBQuery.showAllevents(conec, chain);

	        return full;
}
	
	/**
	 * Obtain all places.
	 *
	 * @param conec the conec
	 * @return the array list
	 */
	public ArrayList<Integer> obtainAllPlaces(Connection conec) {
		//access to JSON 	
	    String chain = "select PlaceId from places;";
	    ArrayList<Integer> pla = new ArrayList<Integer>();
	    
	    pla = PlaceDBQuery.obtainAllPlaces(conec, chain);
	    	
		return pla;
	}
	
	/**
	 * Obtain places name id.
	 *
	 * @param con the con
	 * @return the hash map
	 */
	public HashMap<Integer, String> obtainPlacesNameId(Connection con) {
		HashMap<Integer, String> myPlaces = new HashMap<Integer, String>();
	    String chain = "select PlaceId, Name from places;";
	    
	    myPlaces = PlaceDBQuery.obtainPlacesNameId(con, chain);


		return myPlaces;
	}

					
	/**
	 * Show places id.
	 *
	 * @param conec the conec
	 * @param id the id
	 * @return true, if successful
	 * @see es.ats.codington.festival.interfaces.daos.IPlaceDAO#showPlacesId(java.sql.Connection, int)
	 */
	public boolean showPlacesId(Connection conec, int id) {
		boolean full= false;
		
		//access to JSON 
		JSONQuery reader = new JSONQuery(adress);	
	    String chain = reader.readString(Search_Places_Id);
	        
	    full = PlaceDBQuery.showPlacesId(conec, chain, id);

	    return full;
}


	




}